package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.*;
import com.lzq.course.server.dto.RoleDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.mapper.RoleMapper;
import com.lzq.course.server.mapper.RoleResourceMapper;
import com.lzq.course.server.mapper.RoleUserMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Resource
    private RoleUserMapper roleUserMapper;

    /**
     * example 这个文件很有用
     * 可以帮我们生成where order 查询
     * 不用单独写sql的增删改查
     */
    public void list(PageDto<RoleDto> pageDto) {
        // 分页查询 页码不是从0开始
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());

        // 排序
        RoleExample roleExample = new RoleExample();

        // 查询
        List<Role> roleList = roleMapper.selectByExample(roleExample);

        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        pageDto.setTotal(pageInfo.getTotal());

        List<RoleDto> roleDtoList = CopyUtil.copyList(roleList, RoleDto.class);

        pageDto.setList(roleDtoList);
    }

    /**
     * 保存 修改
     * @param roleDto
     */
    public void save(RoleDto roleDto) {
        //BeanUtils.copyProperties(roleDto, role);
        Role copyRole = CopyUtil.copy(roleDto, Role.class);

        if (StringUtils.isEmpty(roleDto.getId())) {
            // 新增
            // 八位的 id
            copyRole.setId(UuidUtil.getShortUuid());

            Date now = new Date();

            roleMapper.insert(copyRole);
        } else {
            // 编辑
            roleMapper.updateByPrimaryKey(copyRole);
        }
    }

    /**
     * 保存资源ids
     * @param roleDto
     */
    @Transactional
    public void saveResource(RoleDto roleDto) {
        String roleId = roleDto.getId();
        List<String> resourceIds = roleDto.getResourceIds();

        // 先清空当前id的所有资源
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleResourceMapper.deleteByExample(example);
        // 然后遍历数组插入资源id
        for (String resourceId : resourceIds) {
            RoleResource roleResource = new RoleResource();
            roleResource.setId(UuidUtil.getShortUuid());
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceId);
            roleResourceMapper.insert(roleResource);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 角色权限
     * @param roleId
     */
    public List<String> listResource(String roleId) {
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleResource> roleResourceList = roleResourceMapper.selectByExample(example);

        List<String> resourceIds = new ArrayList<>();
        for (RoleResource roleResource : roleResourceList) {
            resourceIds.add(roleResource.getResourceId());
        }

        return resourceIds;
    }

    /**
     * 为用户分角色
     * @param roleDto
     */
    public void saveUser(RoleDto roleDto) {
        List<String> userIds = roleDto.getUserIds();
        String roleId = roleDto.getId();

        RoleUserExample example = new RoleUserExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleUserMapper.deleteByExample(example);

        for (String userId : userIds) {
            RoleUser roleUser = new RoleUser();
            roleUser.setId(UuidUtil.getShortUuid());
            roleUser.setRoleId(roleId);
            roleUser.setUserId(userId);
            roleUserMapper.insert(roleUser);
        }
    }

    /**
     * 获取角色关联的用户
     * @param roleId
     * @return
     */
    public List<String> listUser(String roleId) {
        RoleUserExample example = new RoleUserExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleUser> roleUsers = roleUserMapper.selectByExample(example);

        List<String> userIds = new ArrayList<>();
        for (RoleUser roleUser : roleUsers) {
            userIds.add(roleUser.getUserId());
        }
        return userIds;
    }
}
